package com.crm.member.controller;

import com.crm.member.entity.LayPage;
import com.crm.member.entity.Member;
import com.crm.member.repository.MemberManageRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping
public class MemberManageController {

    @Autowired
    public MemberManageRepository memberManageRepository;

    @GetMapping("/member/{id}")
    public Member getMemberById(@PathVariable Long id){
        Member member = this.memberManageRepository.findOne(id);
        return member;
    }
    @GetMapping("/members/{page}/{size}")
    public LayPage<Member> getMembers(@PathVariable int page, @PathVariable int size, String carId,String name, String phone){
        Page<Member> pagedata = null;
        LayPage<Member> layPagedata = new LayPage<Member>();
        if(size>0) {
            pagedata = memberManageRepository.findAllByCarIdContainsOrNameContainsOrPhoneContains(carId,name, phone, new PageRequest(page, size));
        }
        if(pagedata!=null){
            layPagedata.setCode(0);
            layPagedata.setCount(pagedata.getTotalElements());
            layPagedata.setData(pagedata.getContent());
        }

        return layPagedata;
    }

    @PostMapping("/member/post")
    public Member add(@RequestBody Member member){
        return memberManageRepository.save(member);
    }
}
